﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace AnimalWars2
{
    public delegate void ButtonClickDelegate(Button button);

    public class Button
    {
        protected Game m_game;

        // textures
        protected Texture2D m_buttonTexture;

        protected String m_buttonTextureAssetName;

        // sound
        protected SoundEffect m_mouseOverSoundEffect;
        protected SoundEffectInstance m_mouseOverSoundEffectInstance;
        protected SoundEffect m_mouseClickSoundEffect;

        // mouse
        protected ButtonState m_leftMouseButtonState;
        protected ButtonClickDelegate m_buttonClickDelegate;
        bool m_mouseOver = false;

        // button
        protected int m_leftMargin = 7;
        protected int m_topMargin = 7;
        protected int m_width, m_height;
        protected Color m_buttonColor = Constants.DEFAULT_BUTTON_COLOR;
        protected Color m_buttonOverColor = Constants.DEFAULT_BUTTON_OVER_COLOR;
        protected Color m_ButtonFontColor = Constants.DEFAULT_FONT_COLOR;
        protected Color m_ButtonFontOverColor = Constants.DEFAULT_FONT_OVER_COLOR;
        protected Color m_ButtonFontBorderColor = Constants.DEFAULT_FONT_BORDER_COLOR;
        protected Color m_ButtonFontBorderOverColor = Constants.DEFAULT_FONT_BORDER_OVER_COLOR;
        public int m_x, m_y;
        public String m_text;

        // default constructor
        public Button()
        {

        }

        public Button(Game ticTacToe, String text, int x, int y, String buttonTextureAssetName, ButtonClickDelegate buttonClickDelegate)
        {
            // default values
            m_leftMouseButtonState = ButtonState.Released;

            m_game = ticTacToe;
            m_text = text;
            m_x = x;
            m_y = y;
            m_buttonClickDelegate = buttonClickDelegate;
            m_buttonTextureAssetName = buttonTextureAssetName;
        }

        public void LoadContent()
        {
            if (m_buttonTexture == null)
            {
                m_buttonTexture = m_game.Content.Load<Texture2D>(m_buttonTextureAssetName);
            }

            m_mouseOverSoundEffect = m_game.Content.Load<SoundEffect>(Constants.BUTTON_MOUSEOVER_SOUND_ASSET_NAME);
            m_mouseOverSoundEffectInstance = m_mouseOverSoundEffect.CreateInstance();
            m_mouseClickSoundEffect = m_game.Content.Load<SoundEffect>(Constants.BUTTON_MOUSECLICK_SOUND_ASSET_NAME);

            m_width = m_buttonTexture.Width;
            m_height = m_buttonTexture.Height;
        }

        public void UnloadContent()
        {
            
        }

        protected bool isMouseOver(int mouseX, int mouseY)
        {
            if (mouseX >= m_x && mouseX <= m_x + m_width &&
               mouseY >= m_y && mouseY <= m_y + m_height)
            {
                return true;
            }

            return false;
        }

        protected void mouseHandler()
        {
            MouseState mouse = Mouse.GetState();

            if (isMouseOver(mouse.X, mouse.Y))
            {
                m_buttonColor = Constants.DEFAULT_BUTTON_OVER_COLOR;
                m_ButtonFontColor = Constants.DEFAULT_FONT_OVER_COLOR;
                m_ButtonFontBorderColor = Constants.DEFAULT_FONT_BORDER_OVER_COLOR;

                if (!m_mouseOver && m_mouseOverSoundEffectInstance.State != SoundState.Playing)
                {
                    m_mouseOver = true;

                    if (Constants.PLAY_SOUND)
                    {
                        m_mouseOverSoundEffectInstance.Play();
                    }
                }

                if (m_leftMouseButtonState == ButtonState.Pressed && mouse.LeftButton == ButtonState.Released)
                {
                    if (Constants.PLAY_SOUND)
                    {
                        m_mouseClickSoundEffect.Play();
                    }

                    if (m_buttonClickDelegate != null)
                    {
                        m_buttonClickDelegate(this);
                    }
                }
            }
            else
            {
                m_mouseOver = false;
                m_buttonColor = Constants.DEFAULT_BUTTON_COLOR;
                m_ButtonFontColor = Constants.DEFAULT_FONT_COLOR;
                m_ButtonFontBorderColor = Constants.DEFAULT_FONT_BORDER_COLOR;
            }

            m_leftMouseButtonState = mouse.LeftButton;
        }

        public virtual void Update()
        {
            mouseHandler();
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(m_buttonTexture, new Rectangle(m_x, m_y, m_width, m_height), m_buttonColor);

            if (m_text != null)
            {
                StringUtils.DrawString(m_game.m_Font, spriteBatch, m_text, m_x + m_leftMargin, m_y + m_topMargin, m_ButtonFontColor, m_ButtonFontBorderColor);
            }
        }
    }
}
